import json
import ssl
import time

import requests
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

from geetest.geetest_v3 import GeeTest3
from undetected_chromedriver import Chrome, ChromeOptions

# 测试使用，请求https网站时，禁用SSL证书验证
ssl._create_default_https_context = ssl._create_unverified_context
# 创建ChromeOptions对象，并设置一些选项
options = ChromeOptions()
# 如果要使用无界面访问，开启此参数，默认有窗口界面
# options.add_argument('--headless')
# 将窗口最大化
options.add_argument('--start-maximized')
options.add_argument('--disable-infobars')
# 创建Chrome对象，并使用指定的选项启动Chrome浏览器
driver = Chrome(options=options)



def print_hi(name):
  # Use a breakpoint in the code line below to debug your script.
  print(f'Hi, {name}')  #
  # 最大化
  driver.maximize_window()

  data = requests.get('http://shein.jxwazx.cn/api/rankSyncList')
  while True:
    if data.text == None:
      # 休眠60秒
      time.sleep(60)
      pass
    try:
      updateRankData(data.text)
    except Exception as e:
      print(e)
      pass
    data = requests.get('http://shein.jxwazx.cn/api/rankSyncList')

  # updateRankData(
  #     "https://m.shein.com.mx/Foldable-Shoe-Racks-p-25733784.html?src_identifier=st%3D2%60sc%3Dsh2311013637144032%60sr%3D0%60ps%3D1&src_module=search&src_tab_page_id=page_goods_detail1711085265419&mallCode=2&pageListType=4")


def check(driver):
  driver.execute_script(
      'document.querySelectorAll(\'i.iconfont.icon-close.she-close\')[0]?.click()')
  driver.execute_script(
      'document.querySelectorAll(\'.dialog-top__close-btn\')[0]?.click()')

  # 校验是否有验证码
  WebDriverWait(driver, 1).until(
      EC.visibility_of_element_located(
          (By.CSS_SELECTOR, '.geetest_canvas_bg'))
  )
  print("[step] 已找到验证码")

  geetest = GeeTest3()
  print("[step] 自动验证中")
  flag = geetest.run(driver)

  if flag:
    # 验证通过
    print('[step] 验证通过')
  else:
    print('[step] 验证失败，正在刷新网页重试')
    updateRankData(driver.current_url, True)


def updateRankData(data):
  print('[start] 当前爬取的网页地址是：' + data)
  # 产品
  product = dict()

  driver.get(data)
  print('[step] 网页加载成功：' + data)
  time.sleep(10)
  driver.execute_script(
      'document.querySelector(\'.product-item__ranking-item\')?.click()')
  print('[step] 已点击按钮：' + data)

  try:
    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, '.medal-label-bg'))
    )
  except:
    print('[error] 排名页面加载超时 正在重试')
    pass
  scroll_down(driver)
  # 开始采集数据
  list = driver.find_elements(By.CSS_SELECTOR, '.rank-item')
  productList = []
  for e in list:
    product = dict()
    saleText = e.find_element(By.CSS_SELECTOR,
                              '.sales-content .sales-label-text')
    sku = e.get_attribute('data-sku')
    product['sku'] = sku
    product['nearSaleCount'] = saleText.text
    if product['nearSaleCount'] == "":
      product['nearSaleCount'] ='0 vendido recientemente'
    productList.append(product)
  jsonStr = json.dumps(productList)
  print('[info] jsonStr=' + jsonStr)
  # 直接给post方法中的json参数赋值，赋json类型的数据
  # response = requests.post(url=url, headers=headers, json=productList)
  # page_text = response.text
  # print(page_text)
  url = 'http://shein.jxwazx.cn/api/batchSaveOrUpdate'
  headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'
  }
  print(productList)
  # jsonStr = json.dumps(productList)
  # print(jsonStr)
  # 直接给post方法中的json参数赋值，赋json类型的数据
  response = requests.post(url=url, headers=headers, json=productList)
  page_text = response.text
  print(page_text)


def scroll_down(driver):
  # 执行这段代码，会获取到当前窗口总高度
  js = "return action=document.body.scrollHeight"
  # 初始化现在滚动条所在高度为0
  height = 0
  # 当前窗口总高度
  new_height = driver.execute_script(js)

  # 向下
  while height < new_height:
    # 将滚动条调整至页面底部
    for i in range(height, new_height, 300):
      driver.execute_script('window.scrollTo(0, {})'.format(i))
      # 点击加载更多
      driver.execute_script("document.querySelector('[aria-label=\"View More\"]')?.click()")
      time.sleep(0.5)
      WebDriverWait(driver, 10).until(
          EC.invisibility_of_element_located((By.CSS_SELECTOR, '.S-loading__cur-page-mask'))
      )
    height = new_height
    time.sleep(1)
    new_height = driver.execute_script(js)


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
  print_hi('PyCharm')

# See PyCharm help at https://www.jetbrains.com/help/pycharm/
